package com.qmhl.toolposition.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.qmhl.toolposition.R;
import com.qmhl.toolposition.bean.Chapter;
import com.qmhl.toolposition.bean.ChapterItem;

import java.util.List;

public class ChapterAdapter extends BaseExpandableListAdapter {
    //分组的数据
    private List<Chapter> datas;

    private LayoutInflater inflater;

    private Context mContext;

    public ChapterAdapter(Context context, List<Chapter> datas) {
        this.datas = datas;
        this.mContext = context;
        inflater = LayoutInflater.from(context);
    }

    /**
     * 获取分组的个数
     *
     * @return
     */
    @Override
    public int getGroupCount() {
        return datas.size();
    }

    /**
     * 获取指定分组中子选项的个数
     *
     * @param groupPosition
     * @return
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return datas.get(groupPosition).getList().size();
    }

    /**
     * 获取指定分组的数据
     *
     * @param groupPosition
     * @return
     */
    @Override
    public Object getGroup(int groupPosition) {
        return datas.get(groupPosition);
    }

    /**
     * 获取指定分组中的子选项的数据
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return datas.get(groupPosition).getList().get(childPosition);
    }

    /**
     * 获取指定分组的ID，这个ID必须是唯一的，可以使用分组的position
     *
     * @param groupPosition
     * @return
     */
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    /**
     * 获取子选项的ID，这个ID必须是唯一的，不能使用子条目的position
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /**
     * 分组和子选项是否持有稳定的ID，就是说底层数据的改变会不会影响到它们
     *
     * @return
     */
    @Override
    public boolean hasStableIds() {
        return false;
    }

    /**
     * 获取显示指定分组的视图
     *
     * @param groupPosition
     * @param isExpanded
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder parentViewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.expand_lv_parent, parent, false);
            parentViewHolder = new ParentViewHolder();
            parentViewHolder.tvName = convertView.findViewById(R.id.tv_parent);
            convertView.setTag(parentViewHolder);
        } else {
            parentViewHolder = (ParentViewHolder) convertView.getTag();
        }
        Chapter chapter = datas.get(groupPosition);
        parentViewHolder.tvName.setText(chapter.getName());

        return convertView;
    }

    /**
     * 获取指定分组中的指定子选项的视图
     *
     * @param groupPosition
     * @param childPosition
     * @param isLastChild
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.expand_lv_child, parent, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.tvName = convertView.findViewById(R.id.tv_item);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        ChapterItem chapterItem = datas.get(groupPosition).getList().get(childPosition);
        childViewHolder.tvName.setText(chapterItem.getName());

        return convertView;
    }

    /**
     * 指定位置上的子元素是否可选中，返回true才会相应点击事件
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    public static class ParentViewHolder {
        TextView tvName;
    }

    public static class ChildViewHolder {
        TextView tvName;
    }
}

